/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SPARIAL_DEVICE_MANAGER_H
#define SPARIAL_DEVICE_MANAGER_H

#include <vector>
#include <map>
#include <string>
#include <memory>

#include "device_manager_callback.h"
#include "dm_device_info.h"
#include "dm_subscribe_info.h"
#include "device_manager.h"
#include "dm_app_image_info.h"
#include "icommunicate_interface.h"
#include "spatial_service_timer.h"

namespace OHOS {
namespace Msdp {
class SpatialDeviceManager {
public:
    bool Init();
    int32_t RegisterDevStateCallback();
    int32_t UnRegisterDevStateCallback();
    int32_t StartSpatialDeviceDiscovery(std::string &pkgName, DistributedHardware::DmSubscribeInfo &subscribeInfo);
    int32_t StopSpatialDeviceDiscovery(std::string &pkgName, uint16_t subscribeId);
    void GetTrustedDeviceList();
    int32_t GetOwnDeviceInfo(DistributedHardware::DmDeviceInfo &ownInfo);
    int32_t TransferTrustedDeviceList(std::vector<DistributedHardware::DmDeviceInfo> &deviceList);
    bool GetpkgNameBySubscribeId(int32_t adapterId, std::string &pkgName);
    void SaveDiscoverDeviceInfo(const DistributedHardware::DmDeviceInfo *deviceInfo);
    static void RegisterListoner(const std::shared_ptr<ICommunicateInterface::RemoteListener>& remoteListener);
    static std::shared_ptr<ICommunicateInterface::RemoteListener> remoteListener_;
private:
    std::shared_ptr<DistributedHardware::DiscoveryCallback> callback_ = nullptr;
    std::shared_ptr<DistributedHardware::DmInitCallback> initCallback_ = nullptr;
    std::shared_ptr<DistributedHardware::DeviceStateCallback> stateCb_ = nullptr;
    std::map<std::string, std::vector<std::shared_ptr<DistributedHardware::DmSubscribeInfo>>> subscribeInfos_;
    std::map<std::string, std::shared_ptr<DistributedHardware::DmDeviceInfo>> discoverDeviceInfoMap_;
    std::vector<std::shared_ptr<DistributedHardware::DmDeviceInfo>> discoverDeviceInfoVector_;
    std::vector<DistributedHardware::DmDeviceInfo> deviceList_;
    std::unique_ptr<SpatialServiceTimer> timer_;
    bool deviceStateExist_ {false};
};

class DeviceInitCallbcak : public DistributedHardware::DmInitCallback {
public:
    DeviceInitCallbcak() : DmInitCallback() {}
    virtual ~DeviceInitCallbcak() override {}
    virtual void OnRemoteDied() override;
private:
};

class DeviceDiscoverCallback : public DistributedHardware::DiscoveryCallback {
public:
    friend class SpatialDeviceManager;
    DeviceDiscoverCallback() : DiscoveryCallback() {}
    virtual ~DeviceDiscoverCallback() override {}
    virtual void OnDiscoverySuccess(uint16_t subscribeId) override;
    virtual void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) override;
    virtual void OnDeviceFound(uint16_t subscribeId, const DistributedHardware::DmDeviceInfo &deviceInfo) override;
    void AuthenticateDevice(const DistributedHardware::DmDeviceInfo &deviceInfo);
    void CheckAuthentication(std::string &pinCode);
private:
    std::map<std::string, DistributedHardware::DmDeviceInfo> deviceInfoMap_;
    std::shared_ptr<DistributedHardware::AuthenticateCallback> authCb_;
    std::shared_ptr<DistributedHardware::VerifyAuthCallback> checkAuCb_;
};

class SpatialDeviceStateCallback : public DistributedHardware::DeviceStateCallback {
public:
    SpatialDeviceStateCallback() = default;
    virtual ~SpatialDeviceStateCallback() = default;
    void OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
    void OnDeviceReady(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
    void OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
    void OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override;
};

class SpatialAuthenticateCallback : public DistributedHardware::AuthenticateCallback {
public:
    SpatialAuthenticateCallback() {}
    virtual ~SpatialAuthenticateCallback() {}
    void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status,
                              int32_t reason) override;
};

class SpatialCheckAuthCallback : public DistributedHardware::VerifyAuthCallback {
public:
    SpatialCheckAuthCallback() {}
    virtual ~SpatialCheckAuthCallback() {}
    void OnVerifyAuthResult(const std::string &deviceId, int32_t resultCode, int32_t flag) override;
};
} // namespace Msdp
} // namespace OHOS
#endif // SPARIAL_DEVICE_MANAGER_H